package main

import (
	"encoding/json"

	"jlpay-sdk-go/pkg/jlpay/demo"
	notify2 "jlpay-sdk-go/pkg/jlpay/services/openmerch/notify"
	"jlpay-sdk-go/pkg/jlpay/utils"
)

// 审核结果通知示例
func notifyAuditResultDemo(notifyService *notify2.NotifyService) {
	// 构建通知参数（参考Python版本的参数）
	notifyParam := map[string]interface{}{
		"method":    "POST",
		"uri":       "/notify",
		"alg":       "SM3_WITH_SM2_WITH_DER",
		"sign":      "MEYCIQCpHAZPdXEVbiGW17x9hY0JuEJ4eIX6Uo7Uxk9vczQk1wIhAPXsJHBVRQLiXTUFz15I8UHNLEcgWDwdDx5Uv8GN8Cuo",
		"nonce":     "QpPPCyWZFlgL1dI4MHJHVY37hqMN5YGG",
		"timestamp": "1711536850",
		"body":      `{"apply_id":"b8659ea518d14c99b78b5ad000a0729c","app_id":"1vzougr30f9vcw","audit_status":"01","merch_no":"849584358140108","sign_id":"SIGN123456","sign_status":"01","audit_failure":"","audit_detail":[]}`,
	}

	// 序列化通知参数并打印
	notifyJSON, _ := json.Marshal(notifyParam)
	utils.Debugf("审核结果通知参数: %s", string(notifyJSON))

	// 解析通知
	record, err := notifyService.ParseNotify(notifyParam)
	if err != nil {
		utils.Errorf("审核结果通知解析失败: %v", err)
		return
	}

	if record != nil {
		utils.Info("解析成功")
		utils.Infof("申请ID：%s", record.ApplyID)
		utils.Infof("机构号：%s", record.AppID)
		utils.Infof("审核状态：%s", record.AuditStatus)
		utils.Infof("商户号：%s", record.MerchNo)
		utils.Infof("协议编号：%s", record.SignID)
		utils.Infof("签约状态：%s", record.SignStatus)
		utils.Infof("审核驳回描述：%s", record.AuditFailure)

		if record.AuditDetail != nil && len(record.AuditDetail) > 0 {
			utils.Info("审核详情：")
			for i, detail := range record.AuditDetail {
				utils.Infof("  第%d条详情：", i+1)
				utils.Infof("    字段名称：%s", detail.FieldName)
				utils.Infof("    字段值：%s", detail.FieldValue)
				utils.Infof("    错误信息：%s", detail.ErrorMsg)
			}
		} else {
			utils.Info("审核详情：无")
		}

		// 打印完整响应
		utils.Info("审核结果通知解析完成")
	} else {
		utils.Info("审核结果通知解析结果为空")
	}
}

func main() {
	// 设置日志级别为info，显示详细的调试信息
	utils.SetLogLevel("info")

	utils.Info("=============================")
	utils.Info("===== 审核结果通知示例 开始 =====")
	utils.Info("=============================")

	// 创建配置对象和通知服务
	config := demo.CreateDemoConfig()
	notifyService := notify2.NewNotifyService(config)

	notifyAuditResultDemo(notifyService)

	utils.Info("=============================")
	utils.Info("===== 审核结果通知示例执行完毕 =====")
	utils.Info("=============================")
}
